<template>
  <el-drawer direction="rtl"  :title="dialogTitle" v-model="dialogVisible" size="1200px">
    <!-- 左侧部门树 -->
    <el-row>
      <el-col :span="4" :xs="24">
        <ContentWrap class="h-1/1">
          <CommunityTree @node-click="handleCommunityNodeClick" />
        </ContentWrap>
      </el-col>
      <el-col :span="20" :xs="24">
        <ContentWrap>
          <!-- 搜索工作栏 -->
          <el-form
            class="-mb-15px"
            :model="queryParams"
            ref="queryFormRef"
            :inline="true"
            label-width="68px"
          >
            <el-form-item label="房屋名称" prop="keyWord">
              <el-input
                v-model="queryParams.keyWord"
                placeholder="请输入房屋名称"
                clearable
                @keyup.enter="handleQuery"
                class="!w-240px"
              />
            </el-form-item>
            <el-form-item>
              <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            </el-form-item>
          </el-form>
        </ContentWrap>

        <!-- 列表 -->
        <ContentWrap>
          <el-table v-loading="loading" :data="list">
            <el-table-column label="社区名称" align="center" prop="community.name" width="190" />
            <el-table-column label="房屋地址" align="left" prop="name"/>
            <el-table-column label="操作" align="center" width="160px">
              <template #default="scope">
                <el-button
                  link
                  type="primary"
                  @click="confirmSel( scope.row)"
                >
                  引入
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <Pagination
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </ContentWrap>
      </el-col>
    </el-row>
  </el-drawer>
</template>
<script lang="ts" setup>
import * as HouseRoomApi from '@/api/zwsj/houseRoom'
import CommunityTree from '@/views/zwsj/common/CommunityTree.vue'

defineOptions({ name: 'HouseSelect' })
const emits = defineEmits(['comfirmHouse'])

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  parentId: 0,
  commId: undefined,
  keyWord: undefined
})

const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = '引入房屋'
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询部门列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HouseRoomApi.getHouseRoomPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}


/** 社区被点击 */
const handleCommunityNodeClick = async (row) => {
  queryParams.commId = row.id
  await getList()
}

const confirmSel = async (row: any) => {
  emits('confirmHouse', row)
  dialogVisible.value = false
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
